package com.keti.DaoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.keti.Dao.RecordDao;
import com.keti.pojo.record;

import util.DBUtil;

public class RecordDaoImpl implements RecordDao{

	Connection conn = null;
	PreparedStatement ps =null;
	ResultSet rs = null;
	@Override
	public List<record> querry(int id) {
		List<record> list = new ArrayList<record>();
		conn = DBUtil.getconn();
		try {
			ps = conn.prepareStatement("select *from `record` where uid=?");
			ps.setInt(1, id);
			rs = ps.executeQuery();
			while(rs.next()) {
				id = rs.getInt(1);
				int sid = rs.getInt(2);
				String sname = rs.getString(3);
				int uid = rs.getInt(4);
				String uname = rs.getString(5);
				int scount2 = rs.getInt(6);
				String time = rs.getString(7);
				record r = new record();
				r.setId(id);
				r.setSid(sid);
				r.setSname(sname);
				r.setUid(uid);
				r.setUname(uname);
				r.setScount2(scount2);
				r.setTime(time);
				list.add(r);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public List<record> querryAll() {
		List<record> list = new ArrayList<record>();
		conn = DBUtil.getconn();
		record r = null;
		try {
			ps = conn.prepareStatement("select *from `record`");
			rs = ps.executeQuery();
			while(rs.next()) {
				int id = rs.getInt(1);
				int sid = rs.getInt(2);
				String sname = rs.getString(3);
				int uid = rs.getInt(4);
				String uname = rs.getString(5);
				int scount2 = rs.getInt(6);
				String time = rs.getString(7);
				r = new record();
				r.setId(id);
				r.setSid(sid);
				r.setSname(sname);
				r.setUid(uid);
				r.setUname(uname);
				r.setScount2(scount2);
				r.setTime(time);
				list.add(r);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public void add(record r) {
		conn=DBUtil.getconn();
		String sql="INSERT `record`(sid,sname,uid,uname,scount2,time) VALUE(?,?,?,?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			String[] params={
					String.valueOf(r.getSid()),
					r.getSname(),
					String.valueOf(r.getUid()),
					r.getUname(),
					String.valueOf(r.getScount2()),
					r.getTime()
				};
				DBUtil.exeUpd(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				DBUtil.closeAll(conn, ps, rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	

}
